import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

// 路由配置
const routes: RouteRecordRaw[] = [
  {
    path: "/",
    name: "Login",
    meta: { title: "登录", hidden: true },
    component: () => import("../views/login.vue"),
  },
  {
    path: "/login",
    redirect: "/",
  },
  {
    path: "/index",
    name: "Index",
    meta: { title: "银行管理系统", hidden: true },
    component: () => import("../views/index.vue"),
    redirect: "/dashboard",
    children: [
      {
        path: "/dashboard",
        name: "Dashboard",
        meta: { title: "工作台", icon: "HomeFilled" },
        component: () => import("../views/dashboard/index.vue"),
      },
      {
        path: "/customer",
        name: "Customer",
        meta: { title: "客户管理", icon: "UserFilled" },
        redirect: "/customer/list",
        children: [
          {
            path: "/customerList",
            name: "CustomerList",
            meta: { title: "客户列表", icon: "UserFilled" },
            component: () =>
              import("../views/customerManage/customerList/index.vue"),
          },
          {
            path: "/customerList",
            name: "OverdueCustomer",
            meta: {
              title: "逾期客户",
              icon: "UserFilled",
              query: {
                type: "overdue",
              },
            },
            component: () =>
              import("../views/customerManage/customerList/index.vue"),
          },
          {
            path: "/customerList",
            name: "BadCustomer",
            meta: {
              title: "不良客户",
              icon: "UserFilled",
              query: {
                type: "bad",
              },
            },
            component: () =>
              import("../views/customerManage/customerList/index.vue"),
          },
          {
            path: "/report",
            name: "CustomerReport",
            meta: { title: "客户数据统计", icon: "DocumentCopy" },
            component: () =>
              import("../views/customerManage/dataReport/index.vue"),
          },
        ],
      },
      {
        path: "/loans",
        name: "Loans",
        meta: { title: "贷款管理", icon: "List" },
        redirect: "/loans/list",
        children: [
          {
            path: "/loanList",
            name: "LoanList",
            meta: { title: "贷款列表", icon: "List" },
            component: () => import("../views/loansManage/index.vue"),
          },
          {
            path: "/loanList",
            name: "OverdueLoan",
            meta: {
              title: "逾期贷款",
              icon: "List",
              query: {
                type: "overdue",
              },
            },
            component: () => import("../views/loansManage/index.vue"),
          },
          {
            path: "/loanList",
            name: "UnpaidInterestLoan",
            meta: {
              title: "欠息贷款",
              icon: "List",
              query: {
                type: "unpaidInterest",
              },
            },
            component: () => import("../views/loansManage/index.vue"),
          },
        ],
      },
      {
        path: "/data-report",
        name: "DataReport",
        meta: { title: "数据报表", icon: "DataAnalysis" },
        component: () => import("../views/dataReport/index.vue"),
      },
      {
        path: "/system",
        name: "System",
        meta: { title: "系统管理", icon: "Setting" },
        redirect: "/system/role",
        children: [
          {
            path: "/role",
            name: "Role",
            meta: { title: "角色管理", icon: "UserFilled" },
            component: () => import("../views/system/roleManage/index.vue"),
          },
          {
            path: "/user",
            name: "User",
            meta: { title: "用户管理", icon: "UserFilled" },
            component: () => import("../views/system/userManage/index.vue"),
          },
        ],
      },
      {
        path: "/user-center",
        name: "UserCenter",
        meta: { title: "个人中心", icon: "UserFilled" },
        redirect: "/user-center/personal",
        children: [
          {
            path: "/personal",
            name: "PersonalData",
            meta: { title: "个人资料", icon: "UserFilled" },
            component: () =>
              import("../views/userCenter/personalData/index.vue"),
          },
          {
            path: "/account",
            name: "Account",
            meta: { title: "账户", icon: "UserFilled" },
            component: () => import("../views/userCenter/account/index.vue"),
          },
        ],
      },
    ],
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    meta: { title: "404", hidden: true },
    component: () => import("../views/system/error/404.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  NProgress.start();

  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} | 银行管理系统`;
  }

  next();
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
